using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MacomberMap.Common.Events
{
    /// <summary>
    /// This class holds information on a system exception
    /// </summary>
    public class MM_Event_SystemError: MM_Event
    {
        #region Variable declarations
        /// <summary>The error</summary>
        public Exception Error;
        #endregion

        /// <summary>
        /// Initialize a new system error
        /// </summary>
        /// <param name="Error"></param>
        public MM_Event_SystemError(Exception Error)
        {
            this.Error = Error;
        }

        #region UI interactions
        /// <summary>
        /// Compare two system errors
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override int CompareTo(object obj)
        {
            Exception OtherError = (obj as MM_Event_SystemError).Error;
            return Error.Message.CompareTo(OtherError.Message);
        }

        /// <summary>
        /// Build a list view item for this system error
        /// </summary>
        /// <returns></returns>
        public override ListViewItem BuildListViewItem()
        {            
            ListViewItem NewItem = new ListViewItem(Error.Message);
            NewItem.SubItems.Add(this.EventState.ToString());
            NewItem.SubItems.Add(TimeText);
            NewItem.ImageIndex = EventType.Index;
            return NewItem;
        }

        /// <summary>
        /// Display details on the event
        /// </summary>
        /// <param name="OutParent"></param>
        public override void DisplayDetails(Control OutParent)
        {
            TreeView tvOut = new TreeView();
            tvOut.Dock = DockStyle.Fill;

            Exception ex = this.Error;
            TreeNodeCollection BaseNode = this.AddNodeItem("Error", ex.Message, tvOut.Nodes).Nodes;            
            while (ex != null)
            {
                this.AddNodeItem("Source", ex.Source, BaseNode);
                this.AddNodeItem("Stack Trace", ex.StackTrace, BaseNode);
                this.AddNodeItem("Target Site", ex.TargetSite, BaseNode);
                if (ex.InnerException != null)                
                    BaseNode = this.AddNodeItem("Inner Exception", ex.InnerException.Message, BaseNode).Nodes;
                ex = ex.InnerException;                
            }

            OutParent.Controls.Add(tvOut);
        }

        /// <summary>
        /// Update the text for a list view item
        /// </summary>
        /// <param name="lvItem"></param>
        public override void UpdateListViewItem(ListViewItem lvItem)
        {
            lvItem.SubItems[0].Text = Error.Message;
            lvItem.SubItems[1].Text = this.EventState.ToString();
            lvItem.SubItems[2].Text = TimeText;
        }

        /// <summary>
        /// Add menu items to for the item
        /// </summary>
        /// <param name="OutMenu"></param>
        public override void AddMenuItems(ContextMenuStrip OutMenu)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
        
        #endregion
    }    
}
